package ipmsg.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;

import ipmsg.etc.Command;
import ipmsg.etc.GlobalVar;

public class MsgWindow extends javax.swing.JFrame implements java.awt.event.KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1007082817937097270L;

	private ArrayList<String> contacts = new ArrayList<String>();

	private String key;

	InsertFormatText ift;

	private JComboBox GUI_fontColor;
	private JComboBox GUI_fontName;
	private JComboBox GUI_fontSize;
	private JComboBox GUI_fontStyle;
	private JTextPane guiContent;
	private JLabel guiTips;
	private JTextArea guiWtsay;
	private JButton jButton1;

	public MsgWindow(ArrayList<String> contacts) {
		init(contacts);
	}

	public MsgWindow(String to, String msgContent, String from) {
		if (to == null) {
			init(null);
		} else {
			ArrayList<String> tmp = new ArrayList<String>();
			tmp.add(to);
			init(tmp);
		}

		if (msgContent != null) {
			appendNewMsg(msgContent, from);
		}
	}

	private void initComponents() {
		this.jPanel1 = new JPanel();
		this.jPanel3 = new JPanel();
		this.jScrollPane1 = new JScrollPane();
		this.guiContent = new JTextPane();
		this.jPanel2 = new JPanel();
		this.jScrollPane2 = new JScrollPane();
		this.guiWtsay = new JTextArea();
		this.GUI_fontName = new JComboBox();
		this.GUI_fontSize = new JComboBox();
		this.GUI_fontStyle = new JComboBox();
		this.GUI_fontColor = new JComboBox();
		this.jButton1 = new JButton();
		this.jButton2 = new JButton();
		this.jButton3 = new JButton();
		this.guiTips = new JLabel();
		this.jMenuBar1 = new JMenuBar();
		this.jMenu1 = new JMenu();
		this.jMenu2 = new JMenu();

		setDefaultCloseOperation(0);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				MsgWindow.this.formWindowClosing(evt);
			}

		});
		this.jPanel1.setLayout(new java.awt.BorderLayout());

		this.guiContent.setEditable(false);
		this.jScrollPane1.setViewportView(this.guiContent);

		GroupLayout jPanel3Layout = new GroupLayout(this.jPanel3);
		this.jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(this.jScrollPane1, -1, 433, 32767));

		jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(this.jScrollPane1, -1, 227, 32767));

		this.jPanel1.add(this.jPanel3, "Center");

		this.guiWtsay.setColumns(20);
		this.guiWtsay.setRows(5);
		this.jScrollPane2.setViewportView(this.guiWtsay);

		this.GUI_fontName.setModel(new DefaultComboBoxModel(new String[] { "宋体", "黑体", "Dialog", "Gulim" }));

		this.GUI_fontSize.setModel(new DefaultComboBoxModel(new String[] { "14", "18", "22", "30", "36", "42" }));

		this.GUI_fontStyle.setModel(new DefaultComboBoxModel(new String[] { "常规", "斜体", "粗体", "粗斜体" }));

		this.GUI_fontColor.setModel(new DefaultComboBoxModel(new String[] { "黑色", "红色", "蓝色", "黄色", "绿色" }));

		this.jButton1.setText("发送");
		this.jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				MsgWindow.this.jButton1ActionPerformed(evt);
			}

		});
		this.jButton2.setText("取消");
		this.jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				MsgWindow.this.jButton2ActionPerformed(evt);
			}

		});
		this.jButton3.setText("清空");

		this.guiTips.setText(" ");

		GroupLayout jPanel2Layout = new GroupLayout(this.jPanel2);
		this.jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(jPanel2Layout.createSequentialGroup().addComponent(this.GUI_fontName, -2, -1, -2)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(this.GUI_fontSize, -2, -1, -2)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(this.GUI_fontStyle, -2, -1, -2)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(this.GUI_fontColor, -2, -1, -2).addContainerGap(193, 32767))
						.addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
								.addComponent(this.guiTips, -1, 201, 32767)
								.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.jButton3)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jButton2)
								.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jButton1)
								.addGap(29, 29, 29))))
				.addComponent(this.jScrollPane2, -1, 433, 32767));

		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
						.addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(this.GUI_fontName, -2, -1, -2).addComponent(this.GUI_fontSize, -2, -1, -2)
								.addComponent(this.GUI_fontStyle, -2, -1, -2)
								.addComponent(this.GUI_fontColor, -2, -1, -2))
						.addGap(8, 8, 8).addComponent(this.jScrollPane2, -2, -1, -2)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(this.jButton1).addComponent(this.jButton2).addComponent(this.jButton3)
								.addComponent(this.guiTips))
						.addContainerGap(12, 32767)));

		this.jPanel1.add(this.jPanel2, "South");

		this.jMenu1.setText("File");
		this.jMenuBar1.add(this.jMenu1);

		this.jMenu2.setText("Edit");
		this.jMenuBar1.add(this.jMenu2);

		setJMenuBar(this.jMenuBar1);

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jPanel1, -1, 433, 32767));

		layout.setVerticalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jPanel1, -1, 431, 32767));

		Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 441) / 2, (screenSize.height - 485) / 2, 441, 485);
	}

	private void formWindowClosing(WindowEvent evt) {
		exit();
	}

	private void jButton2ActionPerformed(ActionEvent evt) {
		exit();
	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		sendMsg();
	}

	private JButton jButton2;

	private JButton jButton3;

	private JMenu jMenu1;

	private JMenu jMenu2;

	private JMenuBar jMenuBar1;

	private JPanel jPanel1;

	private JPanel jPanel2;

	private JPanel jPanel3;

	private JScrollPane jScrollPane1;

	private JScrollPane jScrollPane2;

	private void init(ArrayList<String> contacts) {
		initComponents();

		this.ift = new InsertFormatText(this.guiContent);

		this.guiWtsay.addKeyListener(this);
		this.guiContent.addKeyListener(this);
		this.GUI_fontColor.addKeyListener(this);
		this.GUI_fontName.addKeyListener(this);
		this.GUI_fontSize.addKeyListener(this);
		this.GUI_fontStyle.addKeyListener(this);

		this.contacts = contacts;
		if (contacts == null) {
			this.key = "broadcast";
			setTitle("广播");
		} else if (contacts.size() > 1) {
			this.key = null;
			setTitle("群发 ( " + this.contacts.size() + " 个用户 )");
		} else {
			this.key = ((String) contacts.get(0));

			setTitle(GlobalVar.getUser(this.key).getName() + "( " + this.key + " )");
		}

		if (this.key != null)
			GlobalVar.addWindow(this.key, this);
		this.guiWtsay.requestFocus();
	}

	/**
	 * 按下一个键然后释放该键后被调用
	 */
	public void keyTyped(KeyEvent e) {
	}

	/**
	 * 按下一个键后被调用
	 */
	public void keyPressed(KeyEvent e) {
		// e.getKeyCode()-返回这个事件中和键相关的整数键
		if (e.isAltDown() && e.getKeyCode() == KeyEvent.VK_S) {
			sendMsg();
		}
	}

	// public void keyReleased(KeyEvent e) {
	//// 返回这个事件中和键相关的字符
	// char c = e.getKeyChar();
	// int modifiers = e.getModifiers();
	//
	// if (GlobalVar.SEND_MSG_STYLE == 0) {
	// if (((modifiers == 2 ? 1 : 0) & (c == '\n' ? 1 : 0)) != 0) {
	// }
	// } else {
	// if (GlobalVar.SEND_MSG_STYLE != '\001')
	// return;
	// if (((modifiers == 0 ? 1 : 0) & (c == '\n' ? 1 : 0)) == 0) {
	// return;
	// }
	// }
	// sendMsg();
	// }

	/**
	 * 释放一个键后被调用
	 */
	public void keyReleased(KeyEvent e) {
	}

	private void sendMsg() {
		String str = this.guiWtsay.getText().trim();
		if (str.isEmpty()) {
			this.guiTips.setText("请输入消息内容！");
			this.guiWtsay.requestFocus();
			return;
		}

		if ((str.length() > 1000)
				&& (str.length() > 1000 - GlobalVar.USER_NAME.length() - GlobalVar.HOST_NAME.length())) {

			this.guiTips.setText("一次性发送字符太多！");
			this.guiWtsay.requestFocus();
			return;
		}

		Command tmpCom = new Command(288);

		tmpCom.setAdditional(str);

		if ((this.key != null) && (this.key.equals("broadcast"))) {
			ipmsg.network.UtilityNet.broadcastUdpPacket(tmpCom);
		} else {
			appendNewMsg(str, null);
			for (int count = 0; count < this.contacts.size(); count++) {
				if (!GlobalVar.ALL_IP_ADDRESS.contains(this.contacts.get(count))) {
					tmpCom.setIp((String) this.contacts.get(count));
					ipmsg.network.UtilityNet.sendUdpPacket(tmpCom);
				}
			}
		}

		this.guiWtsay.setText("");
		this.guiWtsay.requestFocus();
	}

	public void appendNewMsg(String msgContent, String from) {
		String name = from == null ? "自己" : GlobalVar.getUser(from).getName();

		this.ift.insertFormatText(ipmsg.etc.UtilityGlobal.getTime() + " " + name + GlobalVar.NEW_LINE, "黑体", "粗体", 14,
				"蓝色");

		String fonts = (String) this.GUI_fontName.getSelectedItem();
		String style = (String) this.GUI_fontStyle.getSelectedItem();
		int size = Integer.parseInt((String) this.GUI_fontSize.getSelectedItem());

		String color = (String) this.GUI_fontColor.getSelectedItem();

		this.ift.insertFormatText(msgContent + GlobalVar.NEW_LINE + GlobalVar.NEW_LINE, fonts, style, size, color);

		JScrollBar jb = this.jScrollPane1.getVerticalScrollBar();
		jb.setValue(jb.getMaximum());
	}

	private void exit() {
		if (this.key != null)
			GlobalVar.delWindow(this.key);
		dispose();
	}
}
